package com.crk.shixiguanli.service.impl;

import com.crk.shixiguanli.dao.JobDao;
import com.crk.shixiguanli.dao.JobStudentDao;
import com.crk.shixiguanli.dao.UserDao;
import com.crk.shixiguanli.entity.*;
import com.crk.shixiguanli.service.JobService;
import com.crk.shixiguanli.util.Page;
import com.crk.shixiguanli.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author:
 * Date: 2020-04-26
 * Time: 16:34
 */
@Service
public class JobServiceImpl implements JobService {
    @Autowired
    JobDao jobDao;
    @Autowired
    UserDao userDao;
    @Autowired
    JobStudentDao jobStudentDao;

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public ResponseResult queryPage(Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Integer count = jobDao.countByCondition(userinfo.getId());
        page.setAllRow(count);
        List<Job> collegeList  = jobDao.selectByCondition(userinfo.getId(),page.getStart(), page.getSize());
        List<JobVo> jobVoList = new ArrayList<>();
        for (Job job : collegeList) {
            JobVo jobVo = new JobVo();
            jobVo.setId(job.getId());
            jobVo.setStatus(job.getStatus());
            jobVo.setUpdateTime(job.getUpdateTime());
            jobVo.setJobDesc(job.getJobDesc());
            jobVo.setJobName(job.getJobName());
            jobVo.setJobRequirement(job.getJobRequirement());
            jobVo.setJobTreatment(job.getJobTreatment());
            jobVo.setUserName(userDao.selectByPrimaryKey(job.getUpdater()).getUserName());
            jobVoList.add(jobVo);
        }
        return ResponseResult.success(jobVoList);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public ResponseResult queryAll() {
        List<Job> jobs = jobDao.selectAll();
        List<JobVo> jobVoList = new ArrayList<>();
        for (Job job : jobs) {
            JobVo jobVo = new JobVo();
            jobVo.setId(job.getId());
            jobVo.setStatus(job.getStatus());
            jobVo.setUpdateTime(job.getUpdateTime());
            jobVo.setJobDesc(job.getJobDesc());
            jobVo.setJobName(job.getJobName());
            jobVo.setJobRequirement(job.getJobRequirement());
            jobVo.setJobTreatment(job.getJobTreatment());
            jobVo.setUserName(userDao.selectByPrimaryKey(job.getUpdater()).getUserName());
            if (job.getStatus() != 1){
                continue;
            }
            jobVoList.add(jobVo);
        }
        return ResponseResult.success(jobVoList);
    }


    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public ResponseResult queryAllMyJob() {
        List<Job> jobs = jobDao.selectAll();
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        List<JobVo> jobVoList = new ArrayList<>();
        for (Job job : jobs) {
            JobVo jobVo = new JobVo();
            jobVo.setId(job.getId());
            jobVo.setStatus(job.getStatus());
            jobVo.setUpdateTime(job.getUpdateTime());
            jobVo.setJobDesc(job.getJobDesc());
            jobVo.setJobName(job.getJobName());
            jobVo.setJobRequirement(job.getJobRequirement());
            jobVo.setJobTreatment(job.getJobTreatment());
            jobVo.setUserName(userDao.selectByPrimaryKey(job.getUpdater()).getUserName());
            if (job.getStatus() != 1){
                continue;
            }
            if (job.getCreator() != userinfo.getId()){
                continue;
            }
            jobVoList.add(jobVo);
        }
        return ResponseResult.success(jobVoList);
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOne(Integer id) {
        Job job = jobDao.selectByPrimaryKey(id);
        return ResponseResult.success(job);
    }

    /**
     * 查询单个
     *
     * @return
     */
    @Override
    public ResponseResult getMyJob() {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        JobStudent jobStudent = jobStudentDao.selectByStudent(userinfo.getId());
        if (null == jobStudent){
            return ResponseResult.success(null);
        }
        Job job = jobDao.selectByPrimaryKey(jobStudent.getJobId());
        JobVo jobVo = new JobVo();
        jobVo.setId(job.getId());
        jobVo.setStatus(job.getStatus());
        jobVo.setUpdateTime(job.getUpdateTime());
        jobVo.setJobDesc(job.getJobDesc());
        jobVo.setJobName(job.getJobName());
        jobVo.setJobRequirement(job.getJobRequirement());
        jobVo.setJobTreatment(job.getJobTreatment());
        jobVo.setUserName(userDao.selectByPrimaryKey(job.getUpdater()).getUserName());
        jobVo.setUserType(userinfo.getUserType());
        jobVo.setHasJoin(true);
        return ResponseResult.success(jobVo);
    }

    /**
     * 加入岗位
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult joinJob(Integer id) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        JobStudent jobStudent = new JobStudent();
        jobStudent.setJobId(id);
        jobStudent.setStudentId(userinfo.getId());
        int insert = jobStudentDao.insert(jobStudent);
        if(insert>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOneShow(Integer id) {
        Job job = jobDao.selectByPrimaryKey(id);
        JobVo jobVo = new JobVo();
        jobVo.setId(job.getId());
        jobVo.setStatus(job.getStatus());
        jobVo.setUpdateTime(job.getUpdateTime());
        jobVo.setJobDesc(job.getJobDesc());
        jobVo.setJobName(job.getJobName());
        jobVo.setJobRequirement(job.getJobRequirement());
        jobVo.setJobTreatment(job.getJobTreatment());
        jobVo.setUserName(userDao.selectByPrimaryKey(job.getUpdater()).getUserName());
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        jobVo.setUserType(userinfo.getUserType());
        JobStudent jobStudent = jobStudentDao.selectByStudent(userinfo.getId());
        if(jobStudent != null){
            jobVo.setHasJoin(true);
        }else{
            jobVo.setHasJoin(false);
        }
        return ResponseResult.success(jobVo);
    }

    /**
     * 保存数据
     *
     * @param job
     * @return
     */
    @Override
    public ResponseResult save(Job job) {
        if (job.getId() == null){
            job.setStatus(1);
            job.setCreateTime(new Date());
            job.setUpdateTime(new Date());
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            job.setCreator(userinfo.getId());
            job.setUpdater(userinfo.getId());
            jobDao.insert(job);
        }else{
            job.setUpdateTime(new Date());
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            job.setUpdater(userinfo.getId());
            jobDao.updateByPrimaryKey(job);
        }
        return ResponseResult.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        int i = jobDao.deleteByPrimaryKey(id);
        if (i>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 更改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult changeStatus(Integer id, int status) {
        int count = jobDao.changeStatus(id,status);
        if (count>0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }
}
